﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mjRegister.Entity;
using System.Threading;
using mjRegister.BAL;

namespace mjRegister
{
    public partial class AddApp : Form
    {
        private Form2 _ownerForm;
        private AccountInfo _accountInfo;
        IList<Thread> lstThread = new List<Thread>();
        IList<AppInfo> appInfos = new List<AppInfo>();
        public IList<AppInfo> addAppInfos = new List<AppInfo>();

        public AddApp(Form2 ownerForm, AccountInfo accountInfo)
        {
            _ownerForm = ownerForm;

            _accountInfo = accountInfo;

            InitializeComponent();
        }

        private void AddApp_Load(object sender, EventArgs e)
        {
            appInfos = BAL.App.GetAppInfos();

            if (appInfos.Count > 0)
            {
                LoadAppData(appInfos);
                this.label1.Text = "组件数据加载完成！";
            }
            else
                this.label1.Text = "未找到组件数据，请先初始化！";

        }

        delegate void DLoadAppData(IList<AppInfo> appInfos);

        void LoadAppData(IList<AppInfo> appInfos)
        {
            this.listView1.Items.Clear();

            for (int i = 0; i < appInfos.Count; i++)
            {
                AppInfo appInfo = appInfos[i];

                ListViewItem listViewItem = new ListViewItem(
                    new string[]{
                        appInfo.Id.ToString(),
                        appInfo.Name,
                        appInfo.Description
                    });

                listViewItem.Tag = appInfo;

                this.listView1.Items.Add(listViewItem);
            }
        }

        delegate void DSetControlDisabled();
        void SetControlDisabled()
        {
            this.button1.Enabled = false;
            this.button2.Enabled = false;
            this.listView1.Enabled = false;
        }

        delegate void DRestoreControlEnabled();
        void RestoreControlEnabled()
        {
            this.button1.Enabled = true;
            this.button2.Enabled = true;
            this.listView1.Enabled = true;
        }

        delegate void DInit();
        void Init()
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));
            this.Invoke(new DSetStatusText(SetStatusText), "正在进行组件数据初始化，请稍等……");
            User user = new User();
            if (user.Login(_accountInfo))
            {
                IList<AppInfo> appInfos = user.GetAppInfos();
                BAL.App.Init(appInfos);
                this.Invoke(new DLoadAppData(LoadAppData), appInfos);
            }
            this.Invoke(new DSetStatusText(SetStatusText), "组件数据初始化完成！");
            this.Invoke(new DRestoreControlEnabled(RestoreControlEnabled));
        }

        delegate void DSetStatusText(string text);
        void SetStatusText(string text)
        {
            this.label1.Text = text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(Init));
            t.Start();
            lstThread.Add(t);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                this.listView1.Items[i].Checked = this.checkBox1.Checked;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            addAppInfos.Clear();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    ListViewItem listViewItem = this.listView1.Items[i];
                    AppInfo appInfo = (AppInfo)listViewItem.Tag;
                    addAppInfos.Add(appInfo);
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
